package com.yirui.bridge.util;

import java.io.ByteArrayOutputStream;

/**
 * @author Dujie
 * @create 2021/11/24
 * @since 1.0.0
 */
public class HexCodec {
    public static String decode(byte[] result, char splitChar) {
        if(result == null){
            return null;
        }
        StringBuffer hexString = new StringBuffer();
        for(byte b : result){
            if(hexString.length()>0){
                hexString.append(splitChar);
            }
            String hex = Integer.toHexString(0xFF & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }

            hexString.append(hex.toUpperCase());

        }
        return hexString.toString();
    }

    public static byte[] encode(String data) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        for(int i=0;i<data.length();i=i+2){

            try{
                buffer.write((byte) Integer.parseInt(data.substring(i,i+2), 16));
            }catch(Exception ex){
               ex.printStackTrace();
            }
        }

        return buffer.toByteArray();

    }

//    public static void main(String[] args) {
//        byte[] res = encode("A5AC0C001100320000020253");
//        System.out.println(decode(res, ' '));
//    }
}
